package com.hejian.android.a72ggametools.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.hejian.android.a72ggametools.R;
import com.hejian.android.a72ggametools.adapter.GameSearchAdapter;
import com.hejian.android.a72ggametools.adapter.MoneyAdapter;
import com.hejian.android.a72ggametools.bean.GameLike;
import com.hejian.android.a72ggametools.bean.MoneyGame;
import com.hejian.android.a72ggametools.common.Constant;
import com.hejian.android.a72ggametools.imageloader.DoubleCache;
import com.hejian.android.a72ggametools.imageloader.ImageCache;
import com.hejian.android.a72ggametools.imageloader.ImageLoader;
import com.hejian.android.a72ggametools.net.HttpUtils;
import com.hejian.android.a72ggametools.utils.JsonParse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/1/13.
 */
public class SearchActivity  extends AppCompatActivity implements View.OnClickListener {
    private LinearLayout moneyGameLike1;
    private LinearLayout moneyGameLike2;
    private LinearLayout moneyGameLike3;
    private LinearLayout moneyGameLike4;
    private List<GameLike> gameLikeList;
    private ImageLoader imageLoader;
    private ImageCache imageCache = new DoubleCache();
    private Button backButton;
    private EditText searchEdt;
    private Button searchSubmit;
    private LinearLayout gamelikeAll;
    private ListView searchListView;
    private GameSearchAdapter resultAdapter;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        initView();
        initData();
        setListener();
    }

    private void setListener() {
        setBackButtonListener();
    }

    private void setBackButtonListener() {
        backButton.setOnClickListener(this);
        searchSubmit.setOnClickListener(this);
    }

    private void initData() {
        initGameLike();
    }

    private void initGameLike() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                getGamelikeData();
            }
        }).start();
    }

    private void getGamelikeData() {
        Map<String,String> map = new HashMap<String,String>();
        map.put("platform","2");
        String buffer = HttpUtils.doPostRequest(Constant.MONEY_GAME_LIKE,map);
        List<GameLike> list = JsonParse.parseGameLikeJson(buffer);
        Message msg = Message.obtain();
        msg.what = 1;
        msg.obj = list;
        mHandler.sendMessage(msg);
    }

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1){
                imageLoader = ImageLoader.getInstance(getApplicationContext(),imageCache);
                gameLikeList = (List<GameLike>) msg.obj;
                setGameLikeItem(0,moneyGameLike1);
                setGameLikeItem(1,moneyGameLike2);
                setGameLikeItem(2,moneyGameLike3);
                setGameLikeItem(3,moneyGameLike4);
            }
            if (msg.what == 2){
                List<MoneyGame> list = (List<MoneyGame>) msg.obj;
                setResultAdapter(list);
            }
        }


    };

    private void setResultAdapter(List<MoneyGame> list) {
        resultAdapter = new GameSearchAdapter(list,getApplicationContext());
        searchListView.setAdapter(resultAdapter);
    }


    private void setGameLikeItem(int position, LinearLayout moneyGameLike) {
        ((TextView)moneyGameLike.getChildAt(1)).setText(gameLikeList.get(position).getName());
        ((TextView)moneyGameLike.getChildAt(2)).setText(gameLikeList.get(position).getCount_dl()+"次下载");
        imageLoader.displayImage(gameLikeList.get(position).getIcon(), (ImageView) moneyGameLike.getChildAt(0));
    }

    private void initView() {
        moneyGameLike1 = (LinearLayout) findViewById(R.id.money_game_like_01);
        moneyGameLike2 = (LinearLayout) findViewById(R.id.money_game_like_02);
        moneyGameLike3 = (LinearLayout) findViewById(R.id.money_game_like_03);
        moneyGameLike4 = (LinearLayout) findViewById(R.id.money_game_like_04);
        backButton = (Button)findViewById(R.id.game_search_back);
        searchEdt = (EditText)findViewById(R.id.game_search_input);
        searchSubmit = (Button)findViewById(R.id.game_search_submit);
        gamelikeAll = (LinearLayout)findViewById(R.id.money_game_like_all);
        searchListView = (ListView)findViewById(R.id.search_game_listview);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.game_search_back:
                finish();
                break;
            case R.id.game_search_submit:
                loadSearcheResult();
                break;
        }
    }

    private void loadSearcheResult() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                loadResult();
            }
        }).start();
    }

    private void loadResult() {
        Map<String,String> map = new HashMap<String, String>();
        map.put("platform","2");
        map.put("name",searchEdt.getText().toString());
        String buffer = HttpUtils.doPostRequest(Constant.GAME_SEARCH_URL,map);
        List<MoneyGame> gameList = JsonParse.parseMoneyJson(buffer);
        Message msg = Message.obtain();
        msg.what = 2;
        msg.obj = gameList;
        mHandler.sendMessage(msg);
    }
}
